﻿using Microsoft.AspNetCore.Mvc;
using SIMS.Shared.DTO;
using SIMS.WebApi.Models;
using SIMS.WebApi.Services.Users;

namespace SIMS.WebApi.Controllers
{
    /// <summary>
    /// 用户控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly ILogger<UserController> logger;

        private readonly IUserAppService _service;

        public UserController(ILogger<UserController> logger, IUserAppService service)
        {
            this.logger = logger;
            this._service = service;
        }

        /// <summary>
        /// 新增user
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddUser(UserDTO user)
        {
            var data = _service.AddUser(user);
            
            var res = new ResultModel();
            res.code = 1;
            res.msg = "success";
            res.data = data;
            return Ok(res);
        }

        /// <summary>
        /// 删除User
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DeleteUser(int id)
        {
            var data = _service.DeleteUser(id);
            var res = new ResultModel();
            res.code = 1;
            res.msg = "success";
            res.data = data;
            return Ok(res);
        }

        /// <summary>
        /// 获取单个user
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetUser(int id)
        {
            var data = _service.GetUser(id);

            var res = new ResultModel();
            res.code = 1;
            res.msg = "success";
            res.data = data;
            return Ok(res);
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="pageNum"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetUsers(int pageNum, int pageSize,string? userName)
        {
            var data = _service.GetUsers(userName, pageNum, pageSize);
            var res = new ResultModel();
            res.code = 1;
            res.msg = "success";
            res.data = data;
            return Ok(res);
        }

        /// <summary>
        /// 获取用户角色列表
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetUserRoles(int? userId) {
            var data = _service.GetUserRoles(userId);
            var res = new ResultModel();
            res.code = 1;
            res.msg = "success";
            res.data = data;
            return Ok(res);
        }

        /// <summary>
        /// 设置角色
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="roleIds"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult SetUserRoles(int userId, string roleIds) {
            var data = _service.SetUserRoles(userId, roleIds);
            var res = new ResultModel();
            res.code = 1;
            res.msg = "success";
            res.data = data;
            return Ok(res);
        }

        /// <summary>
        /// 修改User
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateUser(UserDTO user)
        {
            var data = _service.UpdateUser(user);
            var res = new ResultModel();
            res.code = 1;
            res.msg = "success";
            res.data = data;
            return Ok(res);
        }
    }
}
